package com.xgpro.mapper.rolemanage;

import com.xgpro.domain.rolemanage.entity.Permissions;
import com.xgpro.domain.rolemanage.entity.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface RoleMapper extends Mapper<Role> {

    @Select("<script>" +
            "SELECT count(1) from t_user_role WHERE role_id = #{roleId}" +
            "</script>")
    int queryCountByRoleId(Integer roleId);


    @Insert("<script>" +
            "insert into t_role_permissions values ( #{roleId},#{perId})" +
            "</script>")
    int assignPers(@Param("roleId") Integer roleId, @Param("perId") Integer perId);

    @Delete("<script>" +
            "delete from t_role_permissions where role_id = #{roleId}" +
            "</script>")
    int deleteRolePer(Integer roleId);


    /**
     * 通过角色id查询角色所拥有的权限
     * @param roleId
     * @return
     */
    @Select("<script>" +
            "SELECT " +
            "t1.id as id, " +
            "t1.permissions_name as permissionsName, " +
            "t1.description as description, " +
            "t1.type as type, " +
            "t1.code as code " +
            "FROM " +
            "t_permissions t1 " +
            "JOIN t_role_permissions t2 ON t1.id = t2.permissions_id " +
            "WHERE " +
            "t2.role_id =#{roleId}" +
            "</script>")
    List<Permissions> findPermListById(Integer roleId);

}
